package com.zhangtingshu.controller;

import com.zhangtingshu.pojo.Comment;
import com.zhangtingshu.service.CommentService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @author zhangtingshu
 * 帖子评论控制器
 */
@Controller
public class CommentController {
    private Logger logger = Logger.getLogger(CommentController.class);

    @Autowired
    private CommentService commentService;

    @RequestMapping("addComment")
    public String addComment(@ModelAttribute Comment comment) {
//        判断用户是否登录
        if (String.valueOf(comment.getUid()).equals("")) {
//          没有登录，转发到登录页面
            return "redirect:/tologin.action";
        }
        logger.info("评论信息：" + comment);
        boolean flag = commentService.insert(comment);
        logger.info(flag ? "评论添加成功" : "评论添加失败");
//        转发到博客信息页
        return "redirect:/findbyid.action?id=" + comment.getIid();
    }

    //    删除评论
    @RequestMapping("/deleteCom")
    public String delete(int id, int iid) {
        commentService.deleteComment(id);
        return "redirect:/findbyid.action?id=" + iid;
    }
}
